package com.vibrationkeyboard.app.ui

import android.os.Bundle
import android.os.VibrationEffect
import android.os.Vibrator
import android.os.VibratorManager
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import com.vibrationkeyboard.app.R
import com.vibrationkeyboard.app.databinding.ActivityVirtualKeyboardBinding

class VirtualKeyboardActivity : AppCompatActivity() {

    private lateinit var binding: ActivityVirtualKeyboardBinding
    private lateinit var vibrator: Vibrator
    private var isShiftPressed = false
    private var isCapsLock = false
    private var isNumberMode = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityVirtualKeyboardBinding.inflate(layoutInflater)
        setContentView(binding.root)

        initializeVibrator()
        setupKeyboardButtons()
    }

    private fun initializeVibrator() {
        vibrator = if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
            val vibratorManager = getSystemService(VIBRATOR_MANAGER_SERVICE) as VibratorManager
            vibratorManager.defaultVibrator
        } else {
            @Suppress("DEPRECATION")
            getSystemService(VIBRATOR_SERVICE) as Vibrator
        }
    }

    private fun setupKeyboardButtons() {
        // 字母键
        setupLetterButton(binding.btnA, "a", "A")
        setupLetterButton(binding.btnB, "b", "B")
        setupLetterButton(binding.btnC, "c", "C")
        setupLetterButton(binding.btnD, "d", "D")
        setupLetterButton(binding.btnE, "e", "E")
        setupLetterButton(binding.btnF, "f", "F")
        setupLetterButton(binding.btnG, "g", "G")
        setupLetterButton(binding.btnH, "h", "H")
        setupLetterButton(binding.btnI, "i", "I")
        setupLetterButton(binding.btnJ, "j", "J")
        setupLetterButton(binding.btnK, "k", "K")
        setupLetterButton(binding.btnL, "l", "L")
        setupLetterButton(binding.btnM, "m", "M")
        setupLetterButton(binding.btnN, "n", "N")
        setupLetterButton(binding.btnO, "o", "O")
        setupLetterButton(binding.btnP, "p", "P")
        setupLetterButton(binding.btnQ, "q", "Q")
        setupLetterButton(binding.btnR, "r", "R")
        setupLetterButton(binding.btnS, "s", "S")
        setupLetterButton(binding.btnT, "t", "T")
        setupLetterButton(binding.btnU, "u", "U")
        setupLetterButton(binding.btnV, "v", "V")
        setupLetterButton(binding.btnW, "w", "W")
        setupLetterButton(binding.btnX, "x", "X")
        setupLetterButton(binding.btnY, "y", "Y")
        setupLetterButton(binding.btnZ, "z", "Z")

        // 功能键
        binding.btnShift.setOnClickListener {
            toggleShift()
            vibrate()
        }

        binding.btnBackspace.setOnClickListener {
            deleteLastCharacter()
            vibrate()
        }

        binding.btnSpace.setOnClickListener {
            appendText(" ")
            vibrate()
        }

        binding.btnEnter.setOnClickListener {
            appendText("\n")
            vibrate()
        }

        binding.btnClear.setOnClickListener {
            clearText()
            vibrate()
        }

        binding.btn123.setOnClickListener {
            toggleNumberMode()
            vibrate()
        }

        // 符号键
        binding.btnComma.setOnClickListener {
            appendText(",")
            vibrate()
        }

        binding.btnPeriod.setOnClickListener {
            appendText(".")
            vibrate()
        }

        binding.btnQuestion.setOnClickListener {
            appendText("?")
            vibrate()
        }

        binding.btnExclamation.setOnClickListener {
            appendText("!")
            vibrate()
        }

        binding.btnAt.setOnClickListener {
            appendText("@")
            vibrate()
        }

        binding.btnHash.setOnClickListener {
            appendText("#")
            vibrate()
        }

        binding.btnDollar.setOnClickListener {
            appendText("$")
            vibrate()
        }

        binding.btnPercent.setOnClickListener {
            appendText("%")
            vibrate()
        }
    }

    private fun setupLetterButton(button: Button, lowercase: String, uppercase: String) {
        button.setOnClickListener {
            val text = if (isShiftPressed || isCapsLock) uppercase else lowercase
            appendText(text)
            vibrate()
            
            // 如果Shift被按下，释放Shift
            if (isShiftPressed) {
                toggleShift()
            }
        }
    }

    private fun appendText(text: String) {
        val currentText = binding.tvInputText.text.toString()
        binding.tvInputText.text = currentText + text
    }

    private fun deleteLastCharacter() {
        val currentText = binding.tvInputText.text.toString()
        if (currentText.isNotEmpty()) {
            binding.tvInputText.text = currentText.substring(0, currentText.length - 1)
        }
    }

    private fun clearText() {
        binding.tvInputText.text = ""
    }

    private fun toggleShift() {
        isShiftPressed = !isShiftPressed
        updateShiftButton()
    }

    private fun toggleNumberMode() {
        isNumberMode = !isNumberMode
        updateNumberMode()
    }

    private fun updateShiftButton() {
        binding.btnShift.setBackgroundResource(
            if (isShiftPressed) R.color.key_pressed else R.color.key_function
        )
    }

    private fun updateNumberMode() {
        binding.btn123.setBackgroundResource(
            if (isNumberMode) R.color.key_pressed else R.color.key_function
        )
        
        // 这里可以切换键盘布局显示数字
        // 简化实现，实际应用中需要动态切换键盘布局
    }

    private fun vibrate() {
        if (vibrator.hasVibrator()) {
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                vibrator.vibrate(VibrationEffect.createOneShot(50, VibrationEffect.DEFAULT_AMPLITUDE))
            } else {
                @Suppress("DEPRECATION")
                vibrator.vibrate(50)
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        // 清理资源
    }
} 